package com.tongtech.system.controller;

import com.tongtech.common.core.web.controller.BaseController;
import com.tongtech.common.core.web.domain.AjaxResult;
import com.tongtech.common.core.web.page.TableDataInfo;
import com.tongtech.common.log.annotation.Log;
import com.tongtech.common.log.enums.BusinessType;
import com.tongtech.common.security.annotation.RequiresPermissions;
import com.tongtech.common.security.utils.SecurityUtils;
import com.tongtech.system.domain.WorkOrderLog;
import com.tongtech.system.service.IWorkOrderLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 工单日志信息
 *
 * @author xingyc
 */
@RestController
@RequestMapping("/workOrderLog")
public class WorkOrderLogController extends BaseController
{
    @Autowired
    private IWorkOrderLogService workOrderLogService;

    /**
     * 获取工单日志列表
     */
    @RequiresPermissions("workOrderLog:info:list")
    @GetMapping("/list")
    public TableDataInfo list(WorkOrderLog workOrderLog)
    {
        startPage();
        List<WorkOrderLog> list = workOrderLogService.selectWorkOrderLogList(workOrderLog);
        return getDataTable(list);
    }

    /**
     * 根据工单日志编号获取详细信息
     */
    @RequiresPermissions("workOrderLog:info:query")
    @GetMapping(value = "/{workOrderLogId}")
    public AjaxResult getInfo(@PathVariable Long workOrderLogId)
    {
        return success(workOrderLogService.selectWorkOrderLogById(workOrderLogId));
    }

    /**
     * 新增工单日志
     */
    @RequiresPermissions("workOrderLog:info:add")
    @Log(title = "工单日志管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WorkOrderLog workOrderLog)
    {
       if(Objects.nonNull(workOrderLog)){
           workOrderLog.setCreateBy(SecurityUtils.getUsername());
       }
        return toAjax(workOrderLogService.insertWorkOrderLog(workOrderLog));
    }

    /**
     * 修改工单日志
     */
    @RequiresPermissions("workOrderLog:info:edit")
    @Log(title = "工单日志管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WorkOrderLog workOrderLog)
    {
        if(Objects.nonNull(workOrderLog)){
            workOrderLog.setUpdateBy(SecurityUtils.getUsername());
        }
        return toAjax(workOrderLogService.updateWorkOrderLog(workOrderLog));
    }

    /**
     * 修改工单日志启动状体
     */
    @RequiresPermissions("workOrderLog:info:edit")
    @Log(title = "工单日志管理", businessType = BusinessType.UPDATE)
    @PutMapping("/logStatus")
    public AjaxResult logStatus(@Validated @RequestBody WorkOrderLog workOrderLog)
    {
        if(Objects.nonNull(workOrderLog)){
            workOrderLog.setUpdateBy(SecurityUtils.getUsername());
        }
        return toAjax(workOrderLogService.logStatus(workOrderLog));
    }

    /**
     * 删除工单日志
     */
    @RequiresPermissions("workOrderLog:info:remove")
    @Log(title = "工单日志管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workOrderLogId}")
    public AjaxResult remove(@PathVariable Long workOrderLogId)
    {
        return toAjax(workOrderLogService.deleteWorkOrderLogById(workOrderLogId));
    }
}
